package com.kk.xhr.web.vo;

import com.kk.xhr.model.dto.PageDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * PageVO
 *
 * @author liujingcheng
 * @version 1.0, 2020/6/16 10:44
 * @since 1.0.0
 */
@ApiModel
@EqualsAndHashCode
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageVO<T> implements Serializable {

    /** 总数据量 */
    @ApiModelProperty(value = "总数据量", example = "130")
    private Integer total;

    /** 当前页数据 */
    @ApiModelProperty(value = "当前页数据", dataType = "List")
    private List<T> rows;

    public static <T> PageVO<T> empty() {
        return new PageVO<>(0, Collections.emptyList());
    }

    /**
     * 将page dto 转为 page vo
     *
     * @param mapper dto转vo的转换函数
     * @param <D>    DTO类型
     * @param <V>    VO类型
     * @return page vo
     */
    public static <D, V> PageVO<V> fromDTO(PageDto<D> dto, Function<D, V> mapper) {
        return new PageVO<>((int) dto.getTotal(), dto.getPageList().stream().map(mapper).collect(Collectors.toList()));
    }
}
